<?php

namespace Home\Controller;

/**
 * 充值中心
 *
 * @author Administrator
 *        
 */
class RechargeController extends BaseController {
	/**
	 * 获取玩家数据
	 */
	public function mb_GetUserData() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$type = $data->type;
		$key = $data->key;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($type == 0) {
			// 按id查
			$where = "id='$key'";
		} else if ($type == 1) {
			// 按账号查
			$where = "name='$key'";
		}
		
		if ($adminData ['level'] == 0) {
			// 总管理员
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$code = $adminData ['code'];
			$subCode = mb_substr ( $code, 0, 5 );
			if ($where != '')
				$where .= ' AND ';
			$where .= "code like '" . $subCode . "___'";
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$code = $adminData ['code'];
			if ($where != '')
				$where .= ' AND ';
			$where .= "code ='$code'";
		}
		
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( $where )->find ();
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	/**
	 * 一键上分
	 */
	public function mb_QuickUpCoins() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$addCoin = $data->coin;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userRechargeLog = M ( 'Userrechargelog' );
		$userInfo = M ( 'Userinfo' );
		if ($adminData ['level'] == 0) {
			// 总管理员
			$userDatas = $userInfo->select ();
			for($i = 0; $i < count ( $userDatas ); $i ++) {
				$id = $userDatas [$i] ['id'];
				$name = $userDatas [$i] ['name'];
				$coin = $userDatas [$i] ['coin'];
				$newCoin = $coin + $addCoin;
				$code = $userDatas [$i] ['code'];
				$handlerName = $adminData ['name'];
				$handlerCode = $adminData ['code'];
				
				$agentData = $adminInfo->where ( "code='code'" )->find ();
				if ($agentData ['sw_upcoin'] == '0')
					continue;
				
				$type = 0;
				$userInfo->save ( array (
						'id' => $id,
						'coin' => $newCoin 
				) );
				
				$userRechargeLog->add ( array (
						'username' => $name,
						'beforecoins' => $coin,
						'coins' => $addCoin,
						'latercoins' => $newCoin,
						'type' => $type,
						'code' => $code,
						'handlername' => $handlerName,
						'handlercode' => $handlerCode,
						'remark' => '一键上分' 
				) );
			}
			
			$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
		} else
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
		return;
	}
	/**
	 * 充值操作（上分，下分，送分）
	 */
	public function mb_RechargeHandle() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$id = $data->id; // 玩家id
		$type = $data->type; // 0：上分，1：下分，2：送分
		$coin = $data->coin;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		$adminId = $adminData ['id'];
		$adminName = $adminData ['name'];
		$adminCode = $adminData ['code'];
		$adminCoin = $adminData ['coin'];
		$adminLevel = $adminData ['level'];
		$adminUpCoin = $adminData ['sw_upcoin'];
		
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "id='$id'" )->find ();
		$userName = $userData ['name'];
		$userCoin = $userData ['coin'];
		$userCode = $userData ['code'];
		$newCoin = $userCoin + $coin;
		
		if ($adminLevel == 0) {
			$agentData = $adminInfo->where ( "code='$userCode'" )->find ();
			if ($agentData ['sw_upcoin'] == 0) {
				$this->mb_OutMessage ( ErrCode_Toast, '当前用户由代理上下分' );
				return;
			}
		} else if ($adminLevel == 1) {
			$agentData = $adminInfo->where ( "code='$userCode'" )->find ();
			if ($agentData ['sw_upcoin'] == 0) {
				$this->mb_OutMessage ( ErrCode_Toast, '当前用户由代理上下分' );
				return;
			}
		} else if ($adminLevel == 2) {
			if ($adminUpCoin == 1) {
				$this->mb_OutMessage ( ErrCode_Toast, '当前代理不能上下分' );
				return;
			}
			if ($userCode != $adminCode) {
				$agentData = $adminInfo->where ( "code='$userCode'" )->find ();
				if ($agentData ['sw_upcoin'] == 0) {
					$this->mb_OutMessage ( ErrCode_Toast, '当前用户由二级代理上下分' );
					return;
				}
			}
		} else if ($adminLevel == 3) {
			if ($adminUpCoin == 1) {
				$this->mb_OutMessage ( ErrCode_Toast, '当前代理不能上下分' );
				return;
			}
		}
		
		// 获取用户游戏状态
		// $userStatus = \WMHandler::mb_GetInstance ()->mb_GetUserGameStatus ( $userName );
		// // 玩家在李逵，摇钱树，金鲨银鲨不能上下分
		// if ($userStatus == 3 || $userStatus == 11 || $userStatus == 12) {
		// 	$this->mb_OutMessage ( ErrCode_Toast, '当前玩家状态不能上下分' );
		// 	return;
		// }
		// 其他游戏可以上分，不能下分
		// if ($type == 1) {
		// if ($userStatus != - 1 && $userStatus != 0) {
		// $this->mb_OutMessage ( ErrCode_Toast, '当前玩家状态不能下分' );
		// return;
		// }
		// }
		
		// $reqMPZZ = new \Req_sfsMPZZ ();
		// if ($this->mb_NotifySFSUserDataChange ( 4, $reqMPZZ, $userName, $coin ) == false)
		// 	return;
		
		// $reqHLSG = new \Req_sfsHLSG ();
		// if ($this->mb_NotifySFSUserDataChange ( 2, $reqHLSG, $userName, $coin ) == false)
		// 	return;
		
		// $reqBJL = new \Req_sfsBJL ();
		// if ($this->mb_NotifySFSUserDataChange ( 5, $reqBJL, $userName, $coin ) == false)
		// 	return;
		
		// $reqMPZZ2 = new \Req_sfsMPZZ2 ();
		// if ($this->mb_NotifySFSUserDataChange ( 13, $reqMPZZ2, $userName, $coin ) == false)
		// 	return;
		
		// 	$reqFSZZ = new \Req_sfsFSZZ();
		// 	if ($this->mb_NotifySFSUserDataChange ( 1, $reqFSZZ, $userName, $coin ) == false)
		// 		return;
		// 	$reqSWZZ = new \Req_sfsSWZZ();
		// 	if ($this->mb_NotifySFSUserDataChange ( 14, $reqSWZZ, $userName, $coin ) == false)
		// 		return;
		
		// $reqCJ = new \Req_sfsCJ ();
		// if ($this->mb_NotifySFSUserDataChange ( 15, $reqCJ, $userName, $coin ) == false)
		// 	return;
		

		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "type='3'" )->select ();
		
		for($i = 0; $i < count ( $gameData ); $i ++) {
			$url = json_decode ( $gameData [$i] ['url'] );
			if (json_last_error () != JSON_ERROR_NONE) {
				continue;
			}
			
			if($url->recharge == 'sfs'){
				$gameId = $gameData [$i] ['id'];
				$roomId = $url->roomid;
				$reqVGs = new \Req_sfsVideoGames($roomId);
				if ($this->mb_NotifySFSUserDataChange ( $id, $reqVGs, $userName, $coin ) == false)
					return;
				
				\Tools::mb_LogResult("视频游戏充值请求一次");
			}
		}
		
		// if ($this->mb_NotifyWMUserDataChange ( 16, $userName, $coin ) == false)
		// 	return;
		// if ($this->mb_NotifyWMUserDataChange ( 17, $userName, $coin ) == false)
		// 	return;
		// if ($this->mb_NotifyWMUserDataChange ( 18, $userName, $coin ) == false)
		// 	return;
		
		if ($newCoin < 0) {
			$this->mb_OutMessage ( ErrCode_Toast, '用户金币不足' );
			return;
		}
		if ($adminLevel == 2 || $adminLevel == 3) {
			
			$adminNewCoin = $adminCoin - $coin;
			if ($adminNewCoin < 0) {
				$this->mb_OutMessage ( ErrCode_Toast, '当前额度不足！' );
				return;
			}
			$adminInfo->save ( array (
					'id' => $adminId,
					'coin' => $adminNewCoin 
			) );
		}
		
		$userInfo->save ( array (
				'id' => $id,
				'coin' => $newCoin 
		) );
		\WMHandler::mb_GetInstance ()->mb_UpdateUserData ( $userName );
		
		$userRechargeLog = M ( 'Userrechargelog' );
		$userRechargeLog->add ( array (
				'username' => $userName,
				'beforecoins' => $userCoin,
				'coins' => $coin,
				'latercoins' => $newCoin,
				'type' => $type,
				'code' => $userCode,
				'handlername' => $adminName,
				'handlercode' => $adminCode 
		) );
		
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	private function mb_NotifySFSUserDataChange($gameId, $reqSFS, $userName, $coin) {
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		if ($gameData ['status'] == 2)
			return true;
		
		$reqData = $this->mb_DecodeSFSData ( $reqSFS->UserTopUp ( $userName, $coin ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return false;
		}
		return true;
	}
	private function mb_NotifyWMUserDataChange($gameId, $userName, $coin) {
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		if ($gameData ['status'] == 2)
			return true;
		
		$reqData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameData ['url'], 'mb_PlayerRecharge', array (
				'name' => $userName,
				'coin' => $coin 
		) ) );
		if ($reqData->m_Result != ErrCode_Normal) {
			$this->mb_OutMessage ( $reqData->m_Result, $reqData->m_Data );
			return false;
		}
		return true;
	}
	/**
	 *
	 * @param unknown $reqData        	
	 * @return \stdClass
	 */
	private function mb_DecodeSFSData($reqData) {
		$data = new \stdClass ();
		if ($reqData == null || $reqData == '') {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$reqJson = json_decode ( $reqData );
		if (json_last_error () != JSON_ERROR_NONE) {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$state = $reqJson->state;
		if ($state == false) {
			$data->state = false;
			$data->msg = $reqJson->msg;
			return $data;
		}
		$data->state = true;
		if (isset ( $reqJson->msg ))
			$data->msg = $reqJson->msg;
		$data->data = $reqJson;
		return $data;
	}
	/**
	 * 解析游戏服务端返回数据
	 *
	 * @param unknown $reqData        	
	 * @return \RepData|mixed
	 */
	private function mb_DecodeWMData($reqData) {
		$data = new \RepData ();
		if ($reqData == null || $reqData == '') {
			$data->m_Result = ErrCode_Toast;
			$data->m_Data = '外部服务器异常，请稍后再试';
			return $data;
		}
		$reqJson = json_decode ( $reqData );
		if (json_last_error () != JSON_ERROR_NONE) {
			$data->m_Result = ErrCode_Toast;
			$data->m_Data = '外部服务器异常，请稍后再试';
			return $data;
		}
		return $reqJson;
	}
}